class Audio < Media
  require "open3"
  IS_WINDOWS = RUBY_PLATFORM =~ /win32/
  EOF = IS_WINDOWS ? "\C-z" : "\C-d"
  LAME_PATH = File.join(RAILS_ROOT, 'lib', 'lame')

  
  file_column :file 
  after_create :convert
  
  def convert
    puts "Preparing to run conversion"
    #Thread.new {
      from = File.join(RAILS_ROOT,'public', 'audio', 'file', file_relative_path(nil))
      to = File.join(RAILS_ROOT, 'public', 'audio', 'file', file_relative_path + '.mp3')
      
#      Open3.popen3("lame -V2 #{from} #{to}") do |stdin, stdout, stderr|
#        puts stdout.readlines.inspect
#      end
      album = if self.artwork.nil?
        self.artist.name
      else
        self.artwork.name
      end
      
      author = if self.artist.nil?
        self.artwork.artist.name
      else
        self.artist.name
      end
      
      title = if self.artist.nil?
        self.artwork.artist.name
      else
        self.artist.name
      end
      
      command = "ffmpeg -i #{from} -ab 64 -album \"#{album}\" -author \"#{author}\" -title \"#{title}\" #{to}"
      puts command
      
      Open3.popen3(command) do |stdin, stdout, stderr|
        puts stdout.readlines.inspect
      end
    #}
  end
end